import { api } from '../../../../config/api';
import { request } from '../../../../utils/request';

const statusMap = {
	default: { text: '去使用', theme: 'primary' },
	useless: { text: '已使用', theme: 'default' },
	disabled: { text: '已过期', theme: 'default' },
	notget: { text: '领取', theme: 'danger' },
};
Component({
	options: {
		addGlobalClass: true,
		multipleSlots: true, // 在组件定义时的选项中启用多slot支持
	},

	externalClasses: ['coupon-class'],

	properties: {
		couponDTO: {
			type: Object,
			value: {}, // 优惠券数据
		},
	},

	data: {
		btnText: '',
		btnTheme: '',
	},

	observers: {
		couponDTO: function (couponDTO) {
			if (!couponDTO) {
				return;
			}
			const statusInfo = statusMap[couponDTO.status];

			this.setData({
				btnText: statusInfo.text,
				btnTheme: statusInfo.theme,
			});
		},
	},

	attached() { },

	methods: {
		// 跳转到详情页
		gotoDetail() {
			if (this.data.couponDTO.status === 'notget') {
				//调用领取接口
				this.collect();
				return;
			}
			wx.navigateTo({
				url: `/pages/coupon/coupon-detail/index?id=${this.data.couponDTO.key}`,
			});
		},

		// 跳转到商品列表
		gotoGoodsList() {
			wx.navigateTo({
				url: `/pages/coupon/coupon-activity-goods/index?id=${this.data.couponDTO.key}`,
			});
		},
		collect() {
			const me = this;
			request.post(`${api.coupon.collect}?couponId=${me.data.couponDTO.key}`)
				.then(res => {
					if (res) {
						me.setData({ 'couponDTO.status': 'default' });
						wx.showToast({
							title: '领取成功',
							icon: 'none',
							duration: 2000
						});
					}
				});
		}
	},
});
